#!/usr/bien/env python

" temperature de curie dans un materiau ferromagnetique"

from ferromag_ising import *

#IsingModel(style = {cold,hot,alea}, display = {0,1}, nb_etat = {0,2}, liberty = {1,2,3}, H = [x,y,0], J=-1, grille = [p,q])


# determine la temperature de curie
def temp_curie() :

	# initialisation
	temp = 1.3
	temp_max = 3.3
	pas = .01
	boucle = 100000
	
	grille = [30,30]
	spins = grille[0] * grille[1]

	

	while temp < temp_max :
		
		# on initialise la magnetisation moyenne a 0
		M_moyen = 0
		
		# incremente la temperature
		temp += pas
		
		# on fait la moyenne sur 5 essais	
		for i in xrange(5) :
	
			ising = IsingModel('cold', 0, 2, 1, [0,0,0], -1, grille)
			ising.temp = temp

			# on lance l'animation
			ising.stop = 0
			ising.monte_carlo(boucle)
		
			# calcule M_moyen
			M_moyen += ising.M_total/spins
		
		print temp, -M_moyen/5


# cyckle d'hysteresis
def hysteresis() :

	# initialisation
	temp = 1.7
	pas = 0.00008
	boucle = 18
	grille = [50,50]
	H_init = -0.8

	H = H_init
	test = 0
	i = 1
	spins = grille[0] * grille[1]


	ising = IsingModel('cold', 0, 2, 1, [0,0,0], -1, grille)
	
	while 1 :
		
		# on lance l'animation
		ising.H = [H,0,0]
		ising.stop = 0
		ising.temp = temp
		ising.monte_carlo(boucle)
	
		# calcule M_moyen
		M_moyen = ising.M_total/spins
		print H, M_moyen
		
		# incremente H
		H += pas
		
		if H >= -H_init : 
			pas *= -1
			test = 1
			
			# condition d'arret
		if test and H <= H_init + pas: 
			break
		
		i +=1
		
		
if __name__=='__main__':
	

	temp_curie()
	#hysteresis()

